//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.payment;

import java.math.BigDecimal;
import java.util.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.persistence.SQLList;
import org.jpgolay.me.util.MEDate;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.payment:
//            Payment
public class PaymentCRUD {

    public PaymentCRUD(Parameter param) {
        this.param = param;
        init();
    }

    private void init() {
        sqlList = new SQLList();
        sqlVerify = new SQLList();
        crud = new ObjectCRUD();
        date = new MEDate();
        green = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/green12x12.png"));
        orange = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/orange12x12.png"));
        red = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/red12x12.png"));
    }

    public Object[][] getDataList(String sql, String prefix) {
        if (sqlList.search(sql)) {
            Object dataDB[][] = sqlList.getRowsData();
            Object data[][] = new Object[dataDB.length][6];
            for (int i = 0; i < dataDB.length; i++) {
                data[i][0] = dataDB[i][0];
                data[i][1] = date.toME((String) dataDB[i][1]);
                data[i][2] = dataDB[i][2];
                data[i][3] = dataDB[i][3];
                data[i][4] = (new BigDecimal((String) dataDB[i][4])).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][5] = getState((String) dataDB[i][0], new BigDecimal((String) dataDB[i][5]), new BigDecimal((String) dataDB[i][4]), prefix);
            }

            return data;
        } else {
            return null;
        }
    }

    public Integer insertPaymentRow(Payment payment) {
        Integer pk = createPayment();
        if (pk != null) {
            payment.setId(pk);
            updatePaymentRow(payment, -2);
        }
        return pk;
    }

    public boolean updatePaymentRow(Payment payment, int row) {
        Payment newPayment = readPayment(payment.getId());
        payment.setUpdateLastTime(newPayment.getUpdateLastTime());
        if (row == -2) {
            paymentList.add(payment);
        }
        return updatePayment(payment);
    }

    public Payment getOldPayment(int index) {
        return (Payment) paymentList.get(index);
    }

    public boolean deletePaymentRow(Integer id, int row) {
        Payment p = readPayment(id);
        if (p != null) {
            paymentList.remove(row);
            return deletePayment(p);
        } else {
            return false;
        }
    }

    public Object[][] getDataPayment(String id, String prefix) {
        Object data[][] = (Object[][]) null;
        Collection list = readAllPayment(id, prefix);
        if (list != null) {
            paymentList = new ArrayList(list);
            Iterator it = paymentList.iterator();
            data = new Object[paymentList.size() + 30][6];
            int line = 0;
            Payment payment = null;
            while (it.hasNext()) {
                payment = (Payment) it.next();
                data[line][0] = payment.getId();
                data[line][1] = payment.getDate();
                if (payment.getMontant() == null) {
                    data[line][2] = null;
                } else {
                    data[line][2] = payment.getMontant().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                }
                data[line][3] = payment.getMode();
                data[line][4] = payment.getNumero();
                data[line][5] = payment.getPointage();
                line++;
            }
            for (int i = line; i < paymentList.size() + 30; i++) {
                data[i][0] = null;
                data[i][1] = null;
                data[i][2] = null;
                data[i][3] = null;
                data[i][4] = null;
                data[i][5] = null;
            }

        } else {
            paymentList = new ArrayList();
            data = new Object[30][6];
            for (int i = 0; i < 30; i++) {
                data[i][0] = null;
                data[i][1] = null;
                data[i][2] = null;
                data[i][3] = null;
                data[i][4] = null;
                data[i][5] = null;
            }

        }
        return data;
    }

    private Icon getState(String id, BigDecimal payment, BigDecimal total, String prefix) {
        if (payment.compareTo(total) == 0) {
            return green;
        }
        if (sqlVerify.search("SELECT id FROM reglements_clients WHERE piece_id ='" + id + "' AND prefix='" + prefix + "'")) {
            return orange;
        } else {
            return red;
        }
    }

    private Payment readPayment(Integer id) {
        return (Payment) crud.read(id, 26);
    }

    private Collection readAllPayment(String idPiece, String prefix) {
        return crud.readAll(idPiece, prefix, 26);
    }

    private boolean updatePayment(Payment data) {
        return crud.update(data, 26);
    }

    private Integer createPayment() {
        return (Integer) crud.create(26, null);
    }

    private boolean deletePayment(Payment data) {
        return crud.delete(data.getId(), data.getUpdateLastTime(), 26);
    }
    private SQLList sqlList;
    private SQLList sqlVerify;
    private ObjectCRUD crud;
    private Icon green;
    private Icon orange;
    private Icon red;
    private MEDate date;
    private Parameter param;
    private ArrayList paymentList;
}
